﻿@using KL.Store.Core;
@{
    Layout = "~/Views/Shared/_LayoutBasic.cshtml"; ;
    ViewBag.Title = "AddProductToCategory";
    //Html.EnableClientValidation(false);
}
@using (Ajax.BeginForm("AddProductToCategory", "Products", new { }, new AjaxOptions { HttpMethod = "Post" }, new { id = "AddProductToCategory", name = "AddProductToCategory", @class = "form-horizontal" }))
{
    <input type="hidden" name="CategoryId" value="@ViewBag.CategoryId" />
    
    <div class="form-group">
        <div class="col-xs-12">
            <br />
            <h3>@ViewBag.CategoryName Kategorisine Ürün Ekle</h3>
        </div>
    </div>
    if (!string.IsNullOrEmpty(ViewBag.CategoryType))
    {
    <input type="hidden" name="GrupTip" value="@ViewBag.CategoryType" />
    }
    else
    {
    <div class="form-group">
        <label for="GrupTip" class="col-sm-2 control-label">Grup Tipi</label>
        <div class="col-sm-10">
            <select class="form-control" name="GrupTip">
                <option value="A">Ana Ürün Grubu</option>
                <option value="F">Fiyat Grubu</option>
                <option value="M">Marka</option>
                <option value="S">Sayfa Grubu</option>
                <option value="D">Diğer</option>
                <option value="Ü">Üst Grup</option>
            </select>
        </div>
    </div>
    }
    <div class="form-group">
        <label for="searchProduct" class="col-sm-2 control-label">Ürün Ara</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="searchProduct" id="searchProduct" placeholder="Ürün adını yazınız">
            <div id="productsearch_search_results" class="window productsearchview" style="display: none;">
                <div id="divProductSearchResult" style="padding: 6px;">
                </div>
            </div>
        </div>
    </div>
    
    <div class="form-group">
        <div id="productsList" class="col-xs-12">
            <ul>
            </ul>
        </div>
        <div id="formResult" class="col-xs-12">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Ekle</button>
        </div>
    </div>
}
<script type="text/javascript">
    $('#live_productsearch_results li a').live('click', function ()
    {
        var selectedId = $(this).data('id');
        var selectedName = $(this).html();
        var alreadyAdded = false;
        $('form#AddProductToCategory #productsList ul li span').each(function ()
        {
            if ($(this).html() == selectedName)
            {
                alreadyAdded = true;
            }
        });
        if (!alreadyAdded)
        {
            $('#productsList ul').append('<li id="product' + selectedId + '"><i class="fa fa-remove" data-delete="' + selectedId + '"></i>&nbsp;<span>' + selectedName + '</span><input type="hidden" name="Products" value="' + selectedId + '"></li>');
        }
        $('#searchProduct').attr('value', '');
        $('#divProductSearchResult').html('');
        return false;
    });
    $('form#AddProductToCategory #productsList ul li i.fa-remove').live('click', function ()
    {
        var toRemove = $(this).data('delete');
        $('form#AddProductToCategory #productsList ul li#product' + toRemove).remove();
        return false;
    });



    $('form[name=AddProductToCategory]').submit(function ()
    {
        $('#formResult').html('<p class="bg-info">Lütfen Bekleyin...</p>');
    });
    function GetSearchProduct()
    {
        var myText = $("#searchProduct").val();
        $.ajax({
            url: '/Search/LiveSearchFull?search=' + myText,
            type: 'POST',
            success: function (result)
            {
                $("#divLoadingGif").css("display", "none");
                var myProductList = $(result);
                var myHtlmString = '';
                $.each(myProductList[0].LiveSearch, function (key, val)
                {
                    myHtlmString += '<li><a href="' + val.LinkUrl + '" data-id="' + val.ProductId + '">' + val.ProductId + ' - ' + val.Barcode + ' - ' + val.ProductName + '</a></li>';
                });
                myHtlmString = '<ul id="live_productsearch_results">' + myHtlmString + '</ul>';
                $("#divProductSearchResult").html(myHtlmString);
            }
        });
    }
    var search = (function ()
    {
        var builder = function ()
        {
            $(document).ready(function ()
            {
                var object = $('#searchProduct', $('.window.productsearchview').hide().parent()).focus(function ()
                {
                    this.active = true;
                    var value = $.trim(this.value);
                    if (value == object.data('empty'))
                    {
                        object.val('').parent().addClass('focus');
                    } else if (value.length >= 3) { search.show(); }
                }).blur(function ()
                {
                    this.active = false;

                    $(this.parentNode).removeClass('focus');
                    if ($.trim(object.val()) == '') { object.val(object.data('empty')); }
                }).keyup(function ()
                {
                    if (object.val().length >= 4)
                    {
                        $("#divLoadingGif").css("display", "block");
                        GetSearchProduct();
                        search.show();
                        $(this.parentNode).removeClass('focus');
                    } else
                    {
                        search.hide();
                        $(this.parentNode).addClass('focus');
                    }
                });

                var parent = object.parents('.container').hover(function () { this.active = true; }, function ()
                {
                    this.active = false;
                    if (!object.prop('active')) { search.hide(); }
                });
            });
        }
        builder.prototype = {
            busy: false,
            show: function () { $('.window.productsearchview').stop(true, true).animate({ opacity: 'show', height: 'show' }, 'fast'); },
            hide: function () { $('.window.productsearchview').stop(true, true).animate({ opacity: 'hide', height: 'hide' }, 'fast'); }
        };

        return new builder();
    })();
    function kategoriSecilmemis()
    {
        $('#formResult').html('<p class="bg-danger">Kategori Seçilmemiş.</p>');
    }
    function grupTipSecilmemis()
    {
        $('#formResult').html('<p class="bg-danger">Grup Tipi Seçilmemiş.</p>');
    }
    function urunSecilmemis()
    {
        $('#formResult').html('<p class="bg-danger">Ürün Seçilmemiş.</p>');
    }
    function urunlerEklendi(sonucMetni)
    {
        $('#formResult').html('<p class="bg-success">' + sonucMetni + '</p>');
        $('#productsList ul').html('');
    }

</script>
